package com.hc.wdj.core.exception;

import com.hc.wdj.core.error.CoreError;
import com.hc.wdj.core.error.HcError;

import java.util.Objects;

/**
 * @author ryanwdj
 * @since 2024/11/15
 */
public class HcException extends RuntimeException {

    private static final long serialVersionUID = 6881834751084971870L;

    private int code;

    public HcException(HcError error){
        super(error.getMessage());
        this.code = error.getCode();
    }

    public HcException(HcError error, String extraMessage){
        super(String.format(error.getMessage(), extraMessage));
        this.code = error.getCode();
    }

    public HcException(Throwable cause){
        super(cause);
    }

    public HcException(String message, Throwable cause){
        super(message, cause);
    }

    public int getCode(){
        return this.code;
    }

    public static void notTrue(boolean flag, String message){
        if(!flag){
            throw new HcException(CoreError.PARAM_ERROR, message);
        }
    }

    public static void notEmpty(Object value, String message){
        if(Objects.isNull(value)){
            throw new HcException(CoreError.PARAM_ERROR, message);
        }
    }

}
